package com.doodle.common.utils;

import android.database.Observable;


public class CustomFolderHelper {
	
	public interface FoldStateObserver {
	     public void onFolderAdded(String folder);
	     public void onFolderRemoved(String folder);
	     public void onFoldersCleared();
	}
	
	private static final FolderChangedObservable mStateObservable = new FolderChangedObservable();
	
	
	public static synchronized boolean checkIsRegister(FoldStateObserver observer) {
		return mStateObservable.checkIsRegister(observer);
	}
	
	public static synchronized void registerStateObserver(FoldStateObserver observer) {
		mStateObservable.registerObserver(observer);
    }
	

    public static synchronized void unregisterStateObserver(FoldStateObserver observer) {
    	mStateObservable.unregisterObserver(observer);
    }
    
    public static void notifyFolderAdded(String folder) {
    	mStateObservable.notifyFolderAdded(folder);
    }
    
    public static void notifyFolderRemoved(String folder) {
    	mStateObservable.notifyFolderRemoved(folder);
    }
    
    public static void notifyFoldersCleared() {
    	mStateObservable.notifyFoldersCleared();
    }
    
	private static class FolderChangedObservable extends Observable<FoldStateObserver> {
		
	    public void notifyFolderAdded(String folder) {
	        synchronized(mObservers) {
	            for (int i = mObservers.size() - 1; i >= 0; i--) {
	                mObservers.get(i).onFolderAdded(folder);
	            }
	        }
	    }
	    
	    public void notifyFolderRemoved(String folder) {
	        synchronized(mObservers) {
	            for (int i = mObservers.size() - 1; i >= 0; i--) {
	                mObservers.get(i).onFolderRemoved(folder);
	            }
	        }
	    }
	    
	    public void notifyFoldersCleared() {
	        synchronized(mObservers) {
	            for (int i = mObservers.size() - 1; i >= 0; i--) {
	                mObservers.get(i).onFoldersCleared();
	            }
	        }
	    }
	    
	    public boolean checkIsRegister(FoldStateObserver observer) {
	    	synchronized(mObservers) {
	    		return mObservers.contains(observer);
	    	}
	    }
	}
}
